package demo.xlj.yuyi.studentapp.myView;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.bean.RecyclerItemBean;
import demo.xlj.yuyi.studentapp.myadapter.TiMiAdapter;
import demo.xlj.yuyi.studentapp.myadapter.TiMiInterface;

/**
 * 作者：项路俊 on 2018/3/7 0007
 * 邮箱：ThanksForCode@gmail.com
 * 介绍：仿照弹窗
 */

public class TiMiDialog extends Dialog {

    private RecyclerView mRecyclerView;
    private View contentView;
    private TiMiAdapter mTiMiAdapter;
    private List<RecyclerItemBean> mList;
    private int sum = 0;
    private int height = 0;
    private GridLayoutManager mGridLayoutManager;
    private int lastPosition=0;
    private TiMiInterface mTiMiInterface;



    public TiMiDialog(@NonNull Context context) {
        super(context);
        contentView = LayoutInflater.from(context).inflate(R.layout.test_layout, null, false);
        mRecyclerView = (RecyclerView) contentView.findViewById(R.id.RecyclerView_My_Test_Ti_Mi);
        setContentView(contentView);
        setCanceledOnTouchOutside(true);
        mGridLayoutManager = new GridLayoutManager(context, 5);
    }

    public TiMiDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    protected TiMiDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public void setTiMiInterface(TiMiInterface tiMiInterface) {
        mTiMiInterface = tiMiInterface;
    }

    /**
     * 展示出当前Dialog窗体
     */
    public void showDialog(int position) {
        Window window = getWindow();
        //设置弹窗动画
        //设置Dialog背景色
        WindowManager.LayoutParams wl = window.getAttributes();
        window.setBackgroundDrawableResource(R.color.white);
        //设置弹窗位置
        wl.gravity = Gravity.BOTTOM;
        wl.width = WindowManager.LayoutParams.MATCH_PARENT;
        wl.height = height;
        window.setAttributes(wl);

        mList.get(lastPosition).setType(0);
        mList.get(position).setType(1);
        mTiMiAdapter.notifyDataSetChanged();
        mRecyclerView.scrollToPosition(position);
        lastPosition = position;
        show();

    }

    /**
     * 配置数据
     */
    private void setRecyclerView() {
        mList = new ArrayList<>();

        for (int i = 0; i < sum; i++) {
            RecyclerItemBean mRecyclerItemBean = new RecyclerItemBean(0,i+"");
             mList.add(mRecyclerItemBean);
        }
        mTiMiAdapter = new TiMiAdapter(mList);
        mRecyclerView.setLayoutManager(mGridLayoutManager);
        mRecyclerView.setAdapter(mTiMiAdapter);
        mTiMiAdapter.addData(mList);

        mTiMiAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Log.d("xxxxxx","获取的数据是+"+position);
                mTiMiInterface.sendPostion(position);
            }
        });

    }

    /**
     * 配置交互
     * sum 是一共有多少个条目
     * height 表示一共要展示多高
     */

    public void setData(int sum, int height) {
        this.sum = sum;
        this.height = height;
        setRecyclerView();
    }


}
